from excel_processor import ExcelProcessor, generate_write_report_data
from ppt_generator import PPTGenerator
from pdf_extractor import PDFExtractor
from image_processor import ImageProcessor
import os

from src.read_excel_report_data import BaseDataProcessor


class NGReportAutomation:
    def __init__(self, config):
        self.config = config

    def process_excel(self):

        # 读取excel基础数据(第一sheet页的内容)
        excel_data = ExcelProcessor(self.config).read_base_data()

        # 数据格式转换为readReportEntity
        convent_read_data = BaseDataProcessor().convent_report_data(excel_data)

        # 执行不同策略,生成WriteReportEntity
        write_report_entity = generate_write_report_data(convent_read_data, self.config)

        return write_report_entity

    def process_pdf(self):
        """处理PDF文件"""
        pdf_path = self.config['pdf_path']
        output_image_dir = self.config['output_image_dir']
        
        if not os.path.exists(output_image_dir):
            os.makedirs(output_image_dir)
            
        pdf_extractor = PDFExtractor(pdf_path)
        if not pdf_extractor.open_pdf():
            return False
            
        # 根据关键词提取图片
        keyword = self.config['pdf_keyword']
        extracted_images = pdf_extractor.extract_image_by_keyword(keyword)
        
        # 保存提取的图片
        for page_num, image in extracted_images:
            output_path = os.path.join(output_image_dir, f"page_{page_num}.png")
            ImageProcessor.save_image(image, output_path)
            
        pdf_extractor.close()
        return extracted_images
        
    def generate_ppt(self, deviation_list, image_paths):
        """生成PPT"""
        ppt_output_path = self.config['ppt_output_path']
        
        ppt_generator = PPTGenerator()
        ppt_generator.add_cover_slide("NG Report", "Generated Automatically")
        
        # 添加偏差请求表
        ppt_generator.add_deviation_table_slide(deviation_list)
        
        # 添加图片幻灯片
        for image_path in image_paths:
            dimension_label = f"Dimension: {os.path.basename(image_path).split('_')[1]}"
            ppt_generator.add_image_slide(image_path, dimension_label)
            
        ppt_generator.save_presentation(ppt_output_path)
        return True
        
    def run(self):
        """运行整个流程"""
        # 处理Excel
        deviation_list = self.process_excel()
        if deviation_list is None or deviation_list.empty:
            print("No NG data found in Excel.")
            return False
            
        # 处理PDF
        extracted_images = self.process_pdf()
        if not extracted_images:
            print("No images extracted from PDF.")
            return False
            
        # 提取图片路径
        image_paths = []
        for page_num, image in extracted_images:
            image_path = os.path.join(self.config['output_image_dir'], f"page_{page_num}.png")
            image_paths.append(image_path)
            
        # 生成PPT
        if self.generate_ppt(deviation_list, image_paths):
            print("PPT generation completed successfully.")
            return True
        else:
            print("Failed to generate PPT.")
            return False